package com.pochogastos.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBManager {

	private static DBManager instancia;
	private static Connection con = null;
/**	
 * DATOS PRODUCCION
 * private static String url = "jdbc:mysql://127.12.42.129:3306/gastos?autoReconnect=true";
 * private static String user = "adminPxnvFMb";
 * private static String pass = "erVjJRqzLFMa";
**/
	


	private DBManager() throws SQLException, NamingException {
		this.createConnection();
	}

	public static DBManager getInstance() throws SQLException, NamingException {
		if (instancia == null)
			instancia = new DBManager();
		return instancia;

	}

	public void createConnection() throws SQLException, NamingException {
		InitialContext initialContext = new InitialContext();
		DataSource ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/gastos");
		con = ds.getConnection();
	}

	public void closeConnection() throws SQLException {
		if (con != null)
			con.close();
	}
	
	public Connection getConnection(){
		return con;
	}
	

}
